/**
 * 
 */
package WSS;

import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import Objetos.Producto;
import extras.ConnectionString;

/**
 * @author ARMGARCES
 *
 */
public class WssInventario {

	private static String serviceUrl;
	private static String serviceNamespace;
	private static String soapAction;
	/**
	 * 
	 * @param cod
	 * @return
	 */
	private static SoapSerializationEnvelope getServicio(int serv,Object[] values){
		try{
			String[] param = getParametros(serv);
			serviceUrl 		 += ConnectionString.getConnectionString();
			serviceNamespace += ConnectionString.getConnectionString();
			soapAction		 += getMethodName(serv) + ConnectionString.getConnectionString();
			SoapObject rpc = new SoapObject(serviceNamespace, getMethodName(serv));
			//
			for(int i=0;i<param.length;i++){
				rpc.addProperty(param[i], values[i]);
			}
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.encodingStyle = SoapSerializationEnvelope.ENC;

			HttpTransport ht = new HttpTransport(serviceUrl);
			ht.debug = true;
			ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 

			ht.call(soapAction, envelope);
			if(envelope.getResponse() !=null){
				return envelope;
			}
			return null;
		}
        catch(Exception ex){
        	// String mensaje = "Error Descargando Lista (categ: "+categ+",pag: "+pag+")";
        	return null;
        }
	}
	
	/**
	 * 
	 * @param codigo
	 * @return
	 */
	public static Producto getProducto(Object codigo){
		try{
			Vector result = new Vector();
			Object[] param = new Object[1];
			param[0] = codigo;
			
			SoapSerializationEnvelope envelope = getServicio(0, param);
			result = (Vector) envelope.getResponse();
			if(result!=null){	
				Producto producto = new Producto(result);			
				return producto;
			}else{
				return null;
			}
		}catch(Exception e){
			
		}
		return null;
	}
	
	/**
	 * 
	 * @param servicio
	 * @return
	 */
	private static String getMethodName(int servicio){
		switch(servicio){
			//
			case 0:
				serviceUrl = "http://digamaracay3.dynalias.com:8080/WSS/Inventario?wsdl";
				serviceNamespace = "http://digamaracay3.dynalias.com:8080/WSS/";
				soapAction = "http://digamaracay3.dynalias.com:8080/WSS/Inventario/";
				return "getProducto";
			//
			case 1:
				break;
			//
			default:
				break;
		}
		return "";
	}
	/**
	 * 
	 * @param servicio
	 * @return
	 */
	private static String[] getParametros(int servicio){
		
		switch(servicio){
		//getProducto
		case 0:
			String[] param = new String[1];
			param[0] = "codigo";
			return param;
		//
		case 1:
			break;
		//
		default:
			break;
	}
		return null;
	}
	
}
